<?php


/**
*打印函数
*/
function P($data){
	if(is_bool($data)){
        var_dump($data);
    } else if(is_null($data)){
        var_dump($data);
    } else {
        echo '<pre style="padding:10px;border-radius:8px;background:pink;font-size:15px;">'. print_r($data,true) . '</pre>';
        
    }
}

/**
*1.加载配置项---C($sysConfig)--C($userConfig),用户配置优先级高于系**统
*2.读取配置项  C(CODE_LEN)
*3.临时动态改变配置项,多种需求 C('CODE_LEN',3)
*4.C(),什么都不传参数，加载全部的配置项
*/
function C($var = NULL, $value = NULL){
    static $config = array();
     //加载配置项
    if(is_array($var)){
    	$config = array_merge($config,array_change_key_case($var,CASE_UPPER));//合并数组。使开发者的配置项目优先  CASE_UPPER转为大写
    	return;
    } 
     
     // 读取或者动态的改变配置项
     if(is_string($var)){
     	$var = strtoupper($var);//转化字符串为大写
          //如果不为空，则有两个参数传递，则就是临时改变配置项
         if(!is_null($value)){
             $config[$var] = $value;
             return;
         }
        return isset($config[$var]) ? $config[$var] : NULL;
     }

     //返回所有的配置项
     if(is_null($var) && is_null($value)){
     	 return $config;
     }
}

//跳转函数
function go($url,$time=0,$msg=''){
     if(!headers_sent()){
        $time == 0 ? header('Location:' . $url) : header("refresh:{$time};url={$url}");
        die($msg);
     } else {
        echo "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
        if($time) die($msg);
     }      
}

function halt($error,$level='ERROR',$type=3,$dest=NULL) {
     if(is_array($error)){ //如果错误是一个数组
       Log::write($error['message'],$level,$type,$dest);  
     } else {
       Log::write($error,$level,$type,$dest); 
     } 

     $e = array();
     //开启DEBUG的时候
     if(DEBUG){
        if(!is_array($error)){
            $trace = debug_backtrace();
            //P($trace);
            $e['message'] = $error;
            $e['file'] = $trace[0]['file'];
            $e['line'] = $trace[0]['line'];
            $e['class'] = isset($trace[0]['class']) ? $trace[0]['class'] : '';
            $e['function'] = isset($trace[0]['function']) ? $trace[0]['function'] : '';
            ob_start();//开启缓冲狱 ,不立即打印    
            debug_print_backtrace();
            $e['trace'] = htmlspecialchars(ob_get_clean()) ;

           } else {
            $e = $error;
        } 
     } else { //关闭DEBUG的时候
        if($url = C('ERROR_URL')){
             go($url);   
         } else {
            $e['message'] = C('ERROR_MSG');
         }
     }

    include DATA_PATH . '/Tpl/halt.html';
    die;
}

//打印框架中所有定义的常量
function print_const(){
    $const = get_defined_constants(true);
    P($const['user']);
}

function M($table){
    $obj = new Model($table);
    return $obj;
}

function W($model){
    $model .= 'Model';
    return new $model;
}

?>